﻿// ———————————————————————–
// <copyright file="IncidentStatus.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// IncidentStatusType.cs:
//
// The status designation for the incident. Structure is TBD.
//
// Project: IC.NET - CAD Library
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Collections.Generic;
using System.Xml;

namespace EDXLSharp.NIEMCADLib
{
  /// <summary>
  /// The status designation for the incident. 
  /// </summary>
  public abstract class IncidentStatusType : ComplexTypeBase
  {
    public IncidentPrimaryStatus IncidentPrimaryStatus;
    public ICollection<IIncidentSecondaryStatus> SecondaryStatuses;

    /// <summary>
    /// Initializes a new instance of IncidentStatus
    /// </summary>
    public IncidentStatusType()
    {
      SecondaryStatuses = new List<IIncidentSecondaryStatus>();
    }

    /// <summary>
    /// Initializes a new instance of IncidentStatus from an XML string
    /// </summary>
    /// <param name="xmlString">XML representation of a IncidentStatus</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xmlString is null</exception>
    /// <exception cref="System.ArgumentException">Thrown if xmlString is not valid XML, or is not a IncidentStatus element</exception>
    public IncidentStatusType(string xmlString)
      : this()
    {
      if (string.IsNullOrWhiteSpace(xmlString))
      {
        throw new ArgumentException("XML must not be null or whitespace", "xmlString");
      }

      XmlDocument doc = new XmlDocument();
      try
      {
        doc.LoadXml(xmlString);
      }
      catch (XmlException xe)
      {
        throw new ArgumentException("Error parsing XML. See inner exception for details", xe);
      }

      this.ReadXML(doc.DocumentElement);
    }

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xwriter is null</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();

      xwriter.WriteStartElement(EDXLConstants.CADPrefix, "IncidentStatus", EDXLConstants.CADNamespace);
      this.IncidentPrimaryStatus.WriteXML(xwriter);
      foreach (IIncidentSecondaryStatus status in SecondaryStatuses)
      {
        status.WriteXML(xwriter);
      }
      xwriter.WriteEndElement();
    }

    /// <summary>
    /// Reads this Object's values from an XML NodeList
    /// </summary>
    /// <param name="rootnode">Root XML Node of the Object Element</param>
    /// <exception cref="System.ArgumentNullException">Thrown if rootnode is null</exception>
    /// <exception cref="System.ArgumentException">Thrown if the rootnode is not the correct type</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }
      if (rootnode.LocalName != "IncidentStatus")
      {
        throw new ArgumentException("rootnode is not a IncidentStatus element");
      }

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        switch (node.LocalName)
        {
          case "IncidentPrimaryStatus":
            this.IncidentPrimaryStatus = new IncidentPrimaryStatus();
            this.IncidentPrimaryStatus.ReadXML(node);
            break;
          case "IncidentEIDDStatus":
            IncidentEIDDStatus ies = new IncidentEIDDStatus();
            ies.ReadXML(node);
            SecondaryStatuses.Add(ies);
            break;
          case "IncidentPulsePointStatus":
            IncidentPulsePointStatus ipps = new IncidentPulsePointStatus();
            ipps.ReadXML(node);
            SecondaryStatuses.Add(ipps);
            break;
          case "IncidentSecondaryStatusText":
            IncidentSecondaryStatusText isst = new IncidentSecondaryStatusText();
            isst.ReadXML(node);
            SecondaryStatuses.Add(isst);
            break;
          default:
            throw new ArgumentException("Unexpected element " + node.LocalName + " in IncidentStatus");
        }
      }
      //TODO: LDY this method does not validate that after it has read something, it is correct. should it? For example, I can send it an empty resourceStatus with no primary incident, and it's fine.
      // I think it should validate so I'm adding validation here.
      // JC - most (or all) implementations of ReadXML in the library do call validate at the end, so this one missing was likely a mistake
      // however, as ReadXml (and WriteXml!) and Validate are both recursive, there could be a minor performance hit on deeply nested items, since they will be validated many times.. perhaps we want to figure out a more efficient way to do this?
      this.Validate();
    }

    /// <summary>
    /// Validates the required fields are properly populated
    /// </summary>
    /// <exception cref="EDXLSharp.NIEMCADLib.ValidationExcetion">Thrown if validation fails</exception>
    public override void Validate()
    {
      if (this.IncidentPrimaryStatus == null)
      {
        throw new ValidationException("IncidentStatus missing reqiured element IncidentPrimaryStatus");
      }
      this.IncidentPrimaryStatus.Validate();

      foreach (IIncidentSecondaryStatus status in SecondaryStatuses)
      {
        status.Validate();
      }
    }
  }
}